#include "../base.h"

#define LOG_TAG "RelativeLayout"

extern jobject getActivity();

static jclass getClass(JNIEnv* env,const char * className)
{
	return (*env)->FindClass(env,className);
}

static jclass getRelativeLayoutClass(JNIEnv* env)
{
	return getClass(env,"android/widget/RelativeLayout");
}

//创建相对布局
RelativeLayout newRelativeLayout()
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getRelativeLayoutClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(Landroid/content/Context;)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,getActivity());
	RelativeLayout view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return view;
}

//删除相对布局
void deleteRelativeLayout(RelativeLayout thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteRelativeLayout thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//创建一个相对布局参数
RLayoutParams newRLayoutParams(int width,int height)
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/widget/RelativeLayout$LayoutParams");
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(II)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,width,height);
	RLayoutParams params = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return params;
}

//删除一个相对布局参数
void deleteRLayoutParams(RLayoutParams thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteRLayoutParams thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//添加布局规则
void RLayoutParams_addRule(RLayoutParams thiz,int verb,int anchor)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_addViewParams thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/widget/RelativeLayout$LayoutParams");
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"addRule","(II)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,verb,anchor);
}

//添加布局规则
void RLayoutParams_addRuleEx(RLayoutParams thiz,int verb)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_addViewParams thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/widget/RelativeLayout$LayoutParams");
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"addRule","(I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,verb);
}
